package com.yin.ketech.common.model;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yin.ketech.enums.ResultCodeEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import org.springframework.http.HttpStatus;

/**
 * @author luzw
 * @version V1.0
 * @className Result
 * @description TODO
 * @date 2020/6/1
 **/
@Getter
@Setter
@NoArgsConstructor
@ApiModel(value = "统一返回的说明")
public class Result<T> {

    @ApiModelProperty(value = "状态码-200:成功，501:失败")
    private Integer status;
    @ApiModelProperty(value = "信息")
    private String message;
    @ApiModelProperty(value = "数据")
    private T data;

    public Result(Integer status, String message, T data) {
        this.status = status;
        this.message = message;
        this.data = data;
    }

    public static Result ok() {
        return ok(HttpStatus.OK.getReasonPhrase());
    }

    public static Result ok(String message) {
        return result(HttpStatus.OK, message);
    }

    public static <T> Result<T> ok(T data) {
        return result(HttpStatus.OK, data);
    }

    public static <T> Result<T> page(IPage<T> data) {
        ResultPage<T> resPage = new ResultPage(data.getTotal(), data.getRecords());
        return resultPage(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), resPage);
    }

    public static Result error(String message) {
        return result(HttpStatus.NOT_IMPLEMENTED, message);
    }

    public static <T> Result error(T data) {
        return result(HttpStatus.NOT_IMPLEMENTED, data);
    }

    public static Result result(Integer status, String message) {
        return result(status, message, null);
    }

    public static Result result(ResultCodeEnum resultCodeEnum) {
        return result(resultCodeEnum, null);
    }

    public static Result result(HttpStatus httpStatus) {
        return result(httpStatus, null);
    }

    public static <T> Result<T> result(HttpStatus httpStatus, T data) {
        return result(httpStatus.value(), httpStatus.getReasonPhrase(), data);
    }

    public static Result result(HttpStatus httpStatus, String message) {
        return result(httpStatus.value(), message, null);
    }

    public static <T> Result<T> result(ResultCodeEnum resultCodeEnum, T data) {
        return result(resultCodeEnum.getStatus(), resultCodeEnum.getMessage(), data);
    }

    public static <T> Result<T> result(Integer status, String message, T data) {
        Result result = new Result(status, message, data);
        return result;
    }

    public static <T> Result<T> resultPage(Integer status, String message, ResultPage<T> data) {
        Result result = new Result(status, message, data);
        return result;
    }
}
